$(function() {

    //show reset password div on login page
    $('.forgotten_pwd_span').click(function(e) {
        e.preventDefault();
        $('.forgotten_pwd').fadeToggle(500);
    });
    //show if clicked on link forgotten pwd
    console.log(document.location.href);
    if(document.location.href.indexOf('p=login&fpwd=true') != -1) {
        $('.forgotten_pwd').show();
    }


    //show add new auto service center div on admin page
    $('.add_auto_service_center').click(function() {
        $('.add_auto_service_center_content').slideToggle(1300);
    });


    //Admin page AJAX

    //delete advertise
    $('.adv_ajaxdelete').click(function() {
        if(confirm('Are you sure you want to delete this Ad? This action cannot be undone.')) {
            var $user_id = $('img', this).attr('data-user');
            var $adv_id = $('img', this).attr('data-id');
            var $adv_type = $('img', this).attr('data-type');
            var $row = $(this).parents('tr');

            $.post("?p=ajax_requests", {
                "method" : 'ajax_adv_delete',
                "adv_id" : $adv_id,
                "adv_type" : $adv_type,
                "user_id" : $user_id
            }, function(data) {
                $('#notifications').html('<ul><li>' + data + '</li></ul>');
                if(data.indexOf('problem') === -1) {
                    $($row).css('backgroundColor', '#ff8289').fadeOut(1500);
                }
            }, 'html');
        }
    });

    //approve advertise
    $('.adv_ajaxapprove').click(function() {
            var $adv_id = $('img', this).attr('data-id');
            var $adv_type = $('img', this).attr('data-type');
            var $img = $('img', this);

            $.post("?p=ajax_requests", {
                "method" : 'ajax_adv_approve',
                "adv_id" : $adv_id,
                "adv_type" : $adv_type
            }, function(data) {
                if(data.indexOf('problem') === -1) {
                    if($($img).attr('src') == 'img/approve.png') {
                        $($img).attr('src', 'img/approve_green.png');
                    } else {
                        $($img).attr('src', 'img/approve.png');
                    }
                } else {
                    $('#notifications').html('<ul><li>' + data + '</li></ul>');
                }
            }, 'html');
    });


    //add add - change upload div background image when pic uploaded
    $('.upload_file_gallery > input[type=file], .upload_file_gallery1 > input[type=file]')
        .change(function() {
        $(this).parent().css('background', 'url("img/picture_uploaded.jpg")');
    });

    //change company logo background on register page
    $('#style_dealer_image_upload > input[type=file]').change(function() {
        $(this).parent().css('background', 'url("img/logo_uploaded.png")');
    });


    //delete images AJAX script for the adv_edit pages
    $('.delete').click(function() {
        var $pic_id = $('.delete').index(this)+1;
        var $adv_id = document.location.href.substr(document.location.href.lastIndexOf('adv_id=')+7);
        if(document.location.href.indexOf('p=adv_edit_parts') != -1 ) {
            var $adv_type = 'part';
        } else {
            var $adv_type = 'car';
        }

        //console.log('$pic_id: '+ $pic_id + '/ $adv_id: '+ $adv_id + '/ $adv_type: '+ $adv_type);
        $(this).siblings('img').remove(); //remove pic

        $.post("?p=ajax_requests", {
            "method" : 'ajax_pic_delete',
            "adv_id" : $adv_id,
            "adv_type" : $adv_type,
            "pic_id" : $pic_id
        });
    });


    //adv_view - switch visible data for seller contacts
    $('.send_email, .for_contact_with_phone').click(function() {
        $('#for_contact > div').toggleClass(' contact_with_phone_email_not_selected');
        $('#contact_phone_dealer').animate({'height': 'toggle'}, 1100);
        $('#contact_form_seller').animate({'height': 'toggle'}, 1100);
    });

});